#clean up
rm(list=ls())

#libraries
library(apsrtable)
library(foreign)

#set working directory
#setwd("/Volumes/MONOGAN/DISSERTATION/partisanship/data/")

#load data
anes.0<-read.dta('anes_timeseries_cdf.dta',convert.factors=F)

#subset by year
anes<-subset(anes.0,subset=VCF0004>=1969&VCF0901b!="99",select=c(VCF0006a,VCF0009x,VCF0004,VCF0901b,VCF0302,VCF0303,VCF0105a))
colnames(anes)<-c('ID','weight','year','state','pid3','pidLean','race')
#table(anes$pidLean,anes$year,useNA="always")

#recode party variable
#anes$dem<-as.numeric(anes$pid3==5)
#anes$rep<-as.numeric(anes$pid3==1)
anes$dem<-as.numeric(anes$pidLean==1)
anes$rep<-as.numeric(anes$pidLean==3)

#recode race variable
anes$white<-as.numeric(anes$race==1)
anes$black<-as.numeric(anes$race==2)
anes$hispanic<-as.numeric(anes$race==5)

#total identifying with one party
anes$total<-anes$dem+anes$rep

#code states based on 1992 election
anes$clinton<-as.numeric(anes$state%in%c('DC','MA','RI','AR','NY','VT','IL','MD','WV','MN','WA','HI','MO','OR','PA','NM','ME','DE','MI','CT','IA','TN','LA','WI','CO','ME','NM','KY','VT','HI','DE','GA','NV','MT','NH'))

#create subsets by race
white<-subset(anes,white==1)
hispanic<-subset(anes,hispanic==1)

###TOTAL POPULATION###
#subsets of states
blue<-subset(anes,clinton==1)
red<-subset(anes,clinton==0 & state!='TX' & state!='CA' & state!='99')
ca<-subset(anes, state=='CA')
tx<-subset(anes, state=='TX')

#create aggregate measures
year<-seq(1970,2012,by=2)[-c(19,21)]
ca.party<-100*as.vector(sapply(split(ca,ca$year),function(x) sum(x$dem*x$weight))/sapply(split(ca,ca$year),function(x) sum(x$total*x$weight)))
tx.party<-100*as.vector(sapply(split(tx,tx$year),function(x) sum(x$dem*x$weight))/sapply(split(tx,tx$year),function(x) sum(x$total*x$weight)))
red.party<-100*as.vector(sapply(split(red,red$year),function(x) sum(x$dem*x$weight))/sapply(split(red,red$year),function(x) sum(x$total*x$weight)))
blue.party<-100*as.vector(sapply(split(blue,blue$year),function(x) sum(x$dem*x$weight))/sapply(split(blue,blue$year),function(x) sum(x$total*x$weight)))
ca.length<-as.vector(by(ca$total,INDICES=ca$year,length))
tx.length<-as.vector(by(tx$total,INDICES=tx$year,length))
red.length<-as.vector(by(red$total,INDICES=red$year,length))
blue.length<-as.vector(by(blue$total,INDICES=blue$year,length))

#DiD models: states or groups
treat<-1
post<-as.numeric(year>1994)

blue.data<-as.data.frame(cbind(year,ca.party,blue.party,treat,post))
colnames(blue.data)[2:3]<-c('state','group')
treat<-0
red.data<-as.data.frame(cbind(year,tx.party,red.party,treat,post))
colnames(red.data)[2:3]<-c('state','group')
total.data<-rbind(blue.data,red.data)

total.state<-lm(state~treat*post,data=total.data,weights=c(ca.length,tx.length)); summary(total.state)
total.group<-lm(group~treat*post,data=total.data,weights=c(blue.length,red.length)); summary(total.group)

#DiD models: red or blue
treat<-1
post<-as.numeric(year>1994)
state.data<-as.data.frame(cbind(year,ca.party,tx.party,treat,post))
colnames(state.data)[2:3]<-c('blue','red')
treat<-0
group.data<-as.data.frame(cbind(year,blue.party,red.party,treat,post))
colnames(group.data)[2:3]<-c('blue','red')
total.red.blue<-rbind(state.data,group.data)

total.blue<-lm(blue~treat*post,data=total.red.blue,weights=c(ca.length,blue.length)); summary(total.blue)
total.red<-lm(red~treat*post,data=total.red.blue,weights=c(tx.length,red.length)); summary(total.red)


###WHITE VOTERS###
#subsets of states
blue<-subset(white,clinton==1)
red<-subset(white,clinton==0 & state!='TX' & state!='CA' & state!='99')
ca<-subset(white, state=='CA')
tx<-subset(white, state=='TX')

#create aggregate measures
year<-seq(1970,2012,by=2)[-c(19,21)]#T=20
ca.party<-100*as.vector(sapply(split(ca,ca$year),function(x) sum(x$dem*x$weight))/sapply(split(ca,ca$year),function(x) sum(x$total*x$weight)))
tx.party<-100*as.vector(sapply(split(tx,tx$year),function(x) sum(x$dem*x$weight))/sapply(split(tx,tx$year),function(x) sum(x$total*x$weight)))
red.party<-100*as.vector(sapply(split(red,red$year),function(x) sum(x$dem*x$weight))/sapply(split(red,red$year),function(x) sum(x$total*x$weight)))
blue.party<-100*as.vector(sapply(split(blue,blue$year),function(x) sum(x$dem*x$weight))/sapply(split(blue,blue$year),function(x) sum(x$total*x$weight)))

#DiD models: states or groups
treat<-1
post<-as.numeric(year>1994)
blue.data<-as.data.frame(cbind(year,ca.party,blue.party,treat,post))
colnames(blue.data)[2:3]<-c('state','group')
treat<-0
red.data<-as.data.frame(cbind(year,tx.party,red.party,treat,post))
colnames(red.data)[2:3]<-c('state','group')
total.data<-rbind(blue.data,red.data)

white.state<-lm(state~treat*post,data=total.data,weights=c(ca.length,tx.length)); summary(white.state)
white.group<-lm(group~treat*post,data=total.data,weights=c(blue.length,red.length)); summary(white.group)

#DiD models: red or blue
treat<-1
post<-as.numeric(year>1994)
state.data<-as.data.frame(cbind(year,ca.party,tx.party,treat,post))
colnames(state.data)[2:3]<-c('blue','red')
treat<-0
group.data<-as.data.frame(cbind(year,blue.party,red.party,treat,post))
colnames(group.data)[2:3]<-c('blue','red')
total.red.blue<-rbind(state.data,group.data)

white.blue<-lm(blue~treat*post,data=total.red.blue,weights=c(ca.length,blue.length)); summary(white.blue)
white.red<-lm(red~treat*post,data=total.red.blue,weights=c(tx.length,red.length)); summary(white.red)

###HISPANIC VOTERS###
#subsets of states
blue<-subset(hispanic,clinton==1)
red<-subset(hispanic,clinton==0 & state!='TX' & state!='CA' & state!='99')
ca<-subset(hispanic, state=='CA')
tx<-subset(hispanic, state=='TX')

#create aggregate measures
year<-seq(1970,2012,by=2)[-c(19,21)]#T=20
ca.party<-100*as.vector(sapply(split(ca,ca$year),function(x) sum(x$dem*x$weight))/sapply(split(ca,ca$year),function(x) sum(x$total*x$weight)))
tx.party<-c(NA,100*as.vector(sapply(split(tx,tx$year),function(x) sum(x$dem*x$weight))/sapply(split(tx,tx$year),function(x) sum(x$total*x$weight))))
red.party<-100*as.vector(sapply(split(red,red$year),function(x) sum(x$dem*x$weight))/sapply(split(red,red$year),function(x) sum(x$total*x$weight)))
blue.party<-100*as.vector(sapply(split(blue,blue$year),function(x) sum(x$dem*x$weight))/sapply(split(blue,blue$year),function(x) sum(x$total*x$weight)))

#DiD models: states or groups
treat<-1
post<-as.numeric(year>1994)
blue.data<-as.data.frame(cbind(year,ca.party,blue.party,treat,post))
colnames(blue.data)[2:3]<-c('state','group')
treat<-0
red.data<-as.data.frame(cbind(year,tx.party,red.party,treat,post))
colnames(red.data)[2:3]<-c('state','group')
total.data<-rbind(blue.data,red.data)

hisp.state<-lm(state~treat*post,data=total.data,weights=c(ca.length,tx.length)); summary(hisp.state)
hisp.group<-lm(group~treat*post,data=total.data,weights=c(blue.length,red.length)); summary(hisp.group)

#DiD models: red or blue
treat<-1
post<-as.numeric(year>1994)
state.data<-as.data.frame(cbind(year,ca.party,tx.party,treat,post))
colnames(state.data)[2:3]<-c('blue','red')
treat<-0
group.data<-as.data.frame(cbind(year,blue.party,red.party,treat,post))
colnames(group.data)[2:3]<-c('blue','red')
total.red.blue<-rbind(state.data,group.data)

hisp.blue<-lm(blue~treat*post,data=total.red.blue,weights=c(ca.length,blue.length)); summary(hisp.blue)
hisp.red<-lm(red~treat*post,data=total.red.blue,weights=c(tx.length,red.length)); summary(hisp.red)

#output tables
apsrtable(hisp.group, white.group, total.group, lev=.2)
apsrtable(hisp.state, white.state, total.state, lev=.2)
apsrtable(hisp.blue, white.blue, total.blue, lev=.2)
apsrtable(hisp.red, white.red, total.red, lev=.2)
